/*
 * main.c
 *
 *  Created on: Jul 11, 2011
 *      Author: alemao
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "debug.h"

#include "data.h"
#include "statistics.h"
#include "teams.h"

#define RANK    8

static int print_list(player **List){
	int i, j;
	char team[20];
	for (i = 0; List[i]; i++)
	{
		libplayer_getteamname(team, List[i]->team, 20);
		printf("Name: %s, Team: %s, Class %d\n", List[i]->name, team, List[i]->class);
		for (  j= 0; j < ROUNDS_TOTAL; j++){
			if (List[i]->statistics[j] && List[i]->statistics[j]->JOGOU)
				printf("Jogou = %d, Valor = %d, Media: %d, Pontuacao: %d, DD = %d, GS = %d\n", List[i]->statistics[j]->JOGOU, List[i]->statistics[j]->PRECO,
						List[i]->statistics[j]->MEDIA, List[i]->statistics[j]->PONTUACAO, List[i]->statistics[j]->DEFESAS_DIFICEIS, List[i]->statistics[j]->JOGOS_SEM_SOFRER_GOLS);
		}
	}
}

static int print_general_statistics(void)
{
	int i;
	player **list;

	list = get_list_byclass(GOLEIRO);
	i = valorizacao_media(list);
	printf("Media de valorizacao dos goleiros: %d\n", i);
	list = get_list_byclass(LATERAL);
	i = valorizacao_media(list);
	printf("Media de valorizacao dos laterais: %d\n", i);
	list = get_list_byclass(ZAGUEIRO);
	i = valorizacao_media(list);
	printf("Media de valorizacao dos zagueiros: %d\n", i);
	list = get_list_byclass(MEIA);
	i = valorizacao_media(list);
	printf("Media de valorizacao dos meias: %d\n", i);
	list = get_list_byclass(ATACANTE);
	i = valorizacao_media(list);
	printf("Media de valorizacao dos atacantes: %d\n", i);
	list = get_list_byclass(TECNICO);
	i = valorizacao_media(list);
	printf("Media de valorizacao dos tecnicos: %d\n", i);

	i = valorizacao_media_emcasa(get_list_byclass(GOLEIRO));
	printf("Media de valorizacao dos goleiros jogando em casa: %d\n", i);
	i = valorizacao_media_emcasa(get_list_byclass(LATERAL));
	printf("Media de valorizacao dos laterais jogando em casa: %d\n", i);
	i = valorizacao_media_emcasa(get_list_byclass(ZAGUEIRO));
	printf("Media de valorizacao dos zagueiros jogando em casa: %d\n", i);
	i = valorizacao_media_emcasa(get_list_byclass(MEIA));
	printf("Media de valorizacao dos meias jogando em casa: %d\n", i);
	i = valorizacao_media_emcasa(get_list_byclass(ATACANTE));
	printf("Media de valorizacao dos atacantes jogando em casa: %d\n", i);
	i = valorizacao_media_emcasa(get_list_byclass(TECNICO));
	printf("Media de valorizacao dos tecnicos jogando em casa: %d\n", i);

	i = valorizacao_media_fora(get_list_byclass(GOLEIRO));
	printf("Media de valorizacao dos goleiros jogando fora: %d\n", i);
	i = valorizacao_media_fora(get_list_byclass(LATERAL));
	printf("Media de valorizacao dos laterais jogando fora: %d\n", i);
	i = valorizacao_media_fora(get_list_byclass(ZAGUEIRO));
	printf("Media de valorizacao dos zagueiros jogando fora: %d\n", i);
	i = valorizacao_media_fora(get_list_byclass(MEIA));
	printf("Media de valorizacao dos meias jogando fora: %d\n", i);
	i = valorizacao_media_fora(get_list_byclass(ATACANTE));
	printf("Media de valorizacao dos atacantes jogando fora: %d\n", i);
	i = valorizacao_media_fora(get_list_byclass(TECNICO));
	printf("Media de valorizacao dos tecnicos jogando fora: %d\n", i);

	i = pontuacao_media(get_list_byclass(GOLEIRO));
	printf("Media de pontuacao dos goleiros: %d\n", i);
	i = pontuacao_media(get_list_byclass(LATERAL));
	printf("Media de pontuacao dos laterais: %d\n", i);
	i = pontuacao_media(get_list_byclass(ZAGUEIRO));
	printf("Media de pontuacao dos zagueiros: %d\n", i);
	i = pontuacao_media(get_list_byclass(MEIA));
	printf("Media de pontuacao dos meias: %d\n", i);
	i = pontuacao_media(get_list_byclass(ATACANTE));
	printf("Media de pontuacao dos atacantes: %d\n", i);
	i = pontuacao_media(get_list_byclass(TECNICO));
	printf("Media de pontuacao dos tecnicos: %d\n", i);
}

static int print_teams_statistics(int rodadas)
{
	int i, j, ret;
	player **list;
	char team[20];
	char class[20];

	for (i = 1; i < TOTALTEAMS; i++)
	{
		libplayer_getteamname(team, i, 20);
		printf("\nEstatisticas do %s\n", team);
		ret = valorizacao_media(get_list_byteam(i));
		printf("\tMedia de valorizacao: %d\n",  ret);
		ret = valorizacao_media_fora(get_list_byteam(i));
		printf("\tMedia de valorizacao jogando fora: %d\n", ret);
		ret = valorizacao_media_emcasa(get_list_byteam(i));
		printf("\tMedia de valorizacao jogando em casa: %d\n", ret);
		ret = pontuacao_media(get_list_byteam(i));
		printf("\tMedia de pontuacao: %d\n", ret);
		printf("\tPor classe:\tTODOS\tFORA\tEM CASA\n", ret);
		for (j = 1; j < CLASS_TOTAL; j++)
		{
			libplayer_getclassname(class, j, 20);
			printf("\t%s:\t", class);
			ret = pontuacao_media(get_list_byteamandclass(i, j));
			printf("\t%d", ret);
			ret = pontuacao_media_fora(get_list_byteamandclass(i, j));
			printf("\t%d", ret);
			ret = pontuacao_media_emcasa(get_list_byteamandclass(i, j));
			printf("\t%d\n", ret);
		}

		for (j=2; j <= rodadas; j++)
		{
			if (get_list_byteam(i)[0]->statistics[j-2])
			{
				if (get_list_byteam(i)[0]->statistics[j-2]->JOGOU_EM_CASA)
				{
					libplayer_getteamname(team, i, 20);
					printf("\t%s vs ", team);
					libplayer_getteamname(team, get_list_byteam(i)[0]->statistics[j-2]->JOGOU_CONTRA, 20);
					printf("%s\n", team);
				} else {
					libplayer_getteamname(team, get_list_byteam(i)[0]->statistics[j-2]->JOGOU_CONTRA, 20);
					printf("\t%s vs ", team);
					libplayer_getteamname(team, i, 20);
					printf("%s\n", team);
				}
                                ret = pontuacao_media2(get_list_byteam(i), get_list_byteam(i)[0]->statistics[j-2]->JOGOU_EM_CASA, j, j);
				printf("\t\trodada: %d, Media: %d\n", j, ret);
			}

		}

		ret = pontuacao_media_fora(get_list_byteam(i));
		printf("\tMedia de pontuacao jogando fora: %d\n", ret);
		ret = pontuacao_media_emcasa(get_list_byteam(i));
		printf("\tMedia de pontuacao jogando em casa: %d\n",  ret);
		ret = custo_medio(get_list_byteam(i));
		printf("\tMedia de custo: %d\n", ret);
	}
}
static int print_bestclassbet_by_team(int rodadas)
{
	int i, j, k, ret;
	char team[20];
	char class[20];
	int best_class[CLASS_TOTAL][RANK] = {};
	int best_teambyclass[CLASS_TOTAL][RANK] = {};
	int best_class_home[CLASS_TOTAL] = {};
	int best_teambyclass_home[CLASS_TOTAL] = {};
	int best_class_out[CLASS_TOTAL] = {};
	int best_teambyclass_out[CLASS_TOTAL] = {};

	for (i = 1; i < TOTALTEAMS; i++)
	{
		for (j = 1; j < CLASS_TOTAL; j++)
		{
			if (get_list_byteamandclass(i, j)[0]->statistics[rodadas-1])
			{
				if (get_list_byteamandclass(i, j)[0]->statistics[rodadas-1]->JOGOU_EM_CASA)
				{
					ret = pontuacao_media_emcasa(get_list_byteamandclass(i, j));
					if (ret > best_class_home[j])
					{
						best_class_home[j] = ret;
						best_teambyclass_home[j] = i;
					}
				}
				else
				{
					ret = pontuacao_media_fora(get_list_byteamandclass(i, j));
					if (ret > best_class_out[j])
					{
						best_class_out[j] = ret;
						best_teambyclass_out[j] = i;
					}
				}
				for ( k=RANK-1; k>=0; k--)
				{
					if (ret > best_class[j][k])
					{
						if (k<RANK-1)
						{
							best_class[j][k+1] = best_class[j][k];
							best_teambyclass[j][k+1] = best_teambyclass[j][k];
						}
						best_class[j][k] = ret;
						best_teambyclass[j][k] = i;
					}
				}
			}
		}
	}

	printf("\n");
	for (j = 1; j < CLASS_TOTAL; j++)
	{
		libplayer_getclassname(class, j, 20);
		libplayer_getteamname(team, best_teambyclass_home[j], 20);
		printf("Melhor %s em casa: %s\n", class, team);
	}
	printf("\n");
	for (j = 1; j < CLASS_TOTAL; j++)
	{
		libplayer_getclassname(class, j, 20);
		libplayer_getteamname(team, best_teambyclass_out[j], 20);
		printf("Melhor %s fora: %s\n", class, team);
	}
	printf("\n");
	for (j = 1; j < CLASS_TOTAL; j++)
	{
		libplayer_getclassname(class, j, 20);
		printf("Melhor %s geral:", class);
		for (k=0;k<RANK;k++)
		{
			libplayer_getteamname(team, best_teambyclass[j][k], 20);
			printf("\t%10s", team);
		}
		printf("\n");
	}
}
static int print_bestplayerbet_by_class(int rodadas)
{
	int i, j, k, ret;
	char class[20];
	char team[20];
	player **list = NULL;
	player **classlist;
	int best_player_home[CLASS_TOTAL] = {};
	int best_player_out[CLASS_TOTAL] = {};
	int best_player_geral[CLASS_TOTAL][RANK] = {};
	int best_player_points_home[CLASS_TOTAL] = {};
	int best_player_points_out[CLASS_TOTAL] = {};
	int best_player_points_geral[CLASS_TOTAL][RANK] = {};

	for (i = 1; i < CLASS_TOTAL; i++)
	{
		classlist = get_list_byclass(i);
		for (j=0; classlist[j]; j++)
		{
			if (classlist[j]->statistics[rodadas-1])
			{
				if (list)
					libplayer_freelist(list);
				list = libplayer_createlist();
				libplayer_appendtolist(&list, classlist[j]);

				if (list[0]->statistics[rodadas-1]->JOGOU_EM_CASA)
				{
					ret = pontuacao_media_emcasa(list);
					if (ret > best_player_points_home[i])
					{
						best_player_points_home[i] = ret;
						best_player_home[i] = j;
					}
				}
				else
				{
					ret = pontuacao_media_fora(list);
					if (ret > best_player_points_out[i])
					{
						best_player_points_out[i] = ret;
						best_player_out[i] = j;
					}
				}
				for ( k=RANK-1; k>=0; k--)
				{
					if (ret > best_player_points_geral[i][k])
					{
						if (k < (RANK-1) )
						{
							best_player_points_geral[i][k+1] = best_player_points_geral[i][k];
							best_player_geral[i][k+1] = best_player_geral[i][k];
						}
						best_player_points_geral[i][k] = ret;
						best_player_geral[i][k] = j;
					}
				}
			}
		}
	}
	printf("\n");
	for (i = 1; i < CLASS_TOTAL; i++)
	{
		classlist = get_list_byclass(i);
		libplayer_getclassname(class, i, 20);
		libplayer_getteamname(team, classlist[best_player_out[i]]->team, 20);
		printf("Melhor %s em casa: %s, time: %s\n", class, classlist[best_player_out[i]]->name, team);
	}
	printf("\n");
	for (i = 1; i < CLASS_TOTAL; i++)
	{
		classlist = get_list_byclass(i);
		libplayer_getclassname(class, i, 20);
		libplayer_getteamname(team, classlist[best_player_out[i]]->team, 20);
		printf("Melhor %s fora: %s, time: %s\n", class, classlist[best_player_out[i]]->name, team);
	}
	printf("\n");
	for (i = 1; i < CLASS_TOTAL; i++)
	{
		classlist = get_list_byclass(i);
		libplayer_getclassname(class, i, 20);
		printf("Melhor %s:\n", class);
		for (k=0;k<RANK;k++)
		{
			libplayer_getteamname(team, classlist[best_player_geral[i][k]]->team, 20);
			printf("\t%2d - %20s\t%10s\tmedia: %d\n", k+1, classlist[best_player_geral[i][k]]->name, team, best_player_points_geral[i][k]);
		}
		printf("\n");
	}
}

/*
 *
 */
static int print_bestclassbet_by_playedagainst(int rodadas)
{
    int i, j, k, ret;
    char team[20];
    char class[20];
    int best_class[CLASS_TOTAL][RANK] = {};
    int best_teambyclass[CLASS_TOTAL][RANK] = {};
    int best_class_home[CLASS_TOTAL] = {};
    int best_teambyclass_home[CLASS_TOTAL] = {};
    int best_class_out[CLASS_TOTAL] = {};
    int best_teambyclass_out[CLASS_TOTAL] = {};
    player **list;

    for (i = 1; i < TOTALTEAMS; i++)
    {
		for (j = 1; j < CLASS_TOTAL; j++)
		{
			list = get_list_byteamandclass(i, j);

			/* busca a primeira estatistica valida da lista*/
			ret = 0;
			for (k = 0; list[k];k++)
				if (list[k]->statistics[rodadas-1])
				{
					ret = 1;
					break;
				}
			/*if (list[0]->statistics[rodadas-1])*/
			if (ret)
			{
				if (list[k]->statistics[rodadas-1]->JOGOU_EM_CASA)
					ret = 1;
				else ret = 0;
				list = get_list_byclass(j);
				if (ret)
				{
					ret = pontuacao_media_contra(i, 1, list);
					if (ret > best_class_home[j])
					{
							best_class_home[j] = ret;
							best_teambyclass_home[j] = i;
					}
				}
				else
				{
					ret = pontuacao_media_contra(i, 0, list);
					if (ret > best_class_out[j])
					{
							best_class_out[j] = ret;
							best_teambyclass_out[j] = i;
					}
				}
				for ( k=RANK-1; k>=0; k--)
				{
					if (ret > best_class[j][k])
					{
							if (k<RANK-1)
							{
									best_class[j][k+1] = best_class[j][k];
									best_teambyclass[j][k+1] = best_teambyclass[j][k];
							}
							best_class[j][k] = ret;
							best_teambyclass[j][k] = i;
					}
				}
			}
		}
    }

    printf("\n");
    for (j = 1; j < CLASS_TOTAL; j++)
    {
            libplayer_getclassname(class, j, 20);
            libplayer_getteamname(team, best_teambyclass_home[j], 20);
            printf("Melhor %s em casa contra: %s\n", class, team);
    }
    printf("\n");
    for (j = 1; j < CLASS_TOTAL; j++)
    {
            libplayer_getclassname(class, j, 20);
            libplayer_getteamname(team, best_teambyclass_out[j], 20);
            printf("Melhor %s fora contra: %s\n", class, team);
    }
    printf("\n");
    for (j = 1; j < CLASS_TOTAL; j++)
    {
            libplayer_getclassname(class, j, 20);
            printf("Melhor %s geral contra:", class);
            for (k=0;k<RANK;k++)
            {
                    libplayer_getteamname(team, best_teambyclass[j][k], 20);
                    printf("\t%10s", team);
            }
            printf("\n");
    }
}

/*
 *
 */
static int print_bestclassbet_by_teamplus(int rodadas)
{
        int i, j, k, ret;
        char team[20];
        char class[20];
        int best_class[CLASS_TOTAL][RANK] = {};
        int best_teambyclass[CLASS_TOTAL][RANK] = {};
        int best_class_home[CLASS_TOTAL] = {};
        int best_teambyclass_home[CLASS_TOTAL] = {};
        int best_class_out[CLASS_TOTAL] = {};
        int best_teambyclass_out[CLASS_TOTAL] = {};
        player **list;

        for (i = 1; i < TOTALTEAMS; i++)
        {
                for (j = 1; j < CLASS_TOTAL; j++)
                {
                        if (get_list_byteamandclass(i, j)[0]->statistics[rodadas-1])
                        {
								list = get_list_byclass(j);
                                if (get_list_byteamandclass(i, j)[0]->statistics[rodadas-1]->JOGOU_EM_CASA)
                                {
                                        ret = pontuacao_media_emcasa(get_list_byteamandclass(i, j));
                                        ret += pontuacao_media_contra(get_list_byteamandclass(i, j)[0]->statistics[rodadas-1]->JOGOU_CONTRA, 0, list);
                                        if (ret > best_class_home[j])
                                        {
                                                best_class_home[j] = ret;
                                                best_teambyclass_home[j] = i;
                                        }
                                }
                                else
                                {
                                        ret = pontuacao_media_fora(get_list_byteamandclass(i, j));
                                        ret += pontuacao_media_contra(get_list_byteamandclass(i, j)[0]->statistics[rodadas-1]->JOGOU_CONTRA, 1, list);
                                        if (ret > best_class_out[j])
                                        {
                                                best_class_out[j] = ret;
                                                best_teambyclass_out[j] = i;
                                        }
                                }
                                for ( k=RANK-1; k>=0; k--)
                                {
                                        if (ret > best_class[j][k])
                                        {
                                                if (k<RANK-1)
                                                {
                                                        best_class[j][k+1] = best_class[j][k];
                                                        best_teambyclass[j][k+1] = best_teambyclass[j][k];
                                                }
                                                best_class[j][k] = ret;
                                                best_teambyclass[j][k] = i;
                                        }
                                }
                        }
                }
        }

        printf("\n");
        for (j = 1; j < CLASS_TOTAL; j++)
        {
                libplayer_getclassname(class, j, 20);
                libplayer_getteamname(team, best_teambyclass_home[j], 20);
                printf("Melhor %s em casa plus: %s\n", class, team);
        }
        printf("\n");
        for (j = 1; j < CLASS_TOTAL; j++)
        {
                libplayer_getclassname(class, j, 20);
                libplayer_getteamname(team, best_teambyclass_out[j], 20);
                printf("Melhor %s fora plus: %s\n", class, team);
        }
        printf("\n");
        for (j = 1; j < CLASS_TOTAL; j++)
        {
                libplayer_getclassname(class, j, 20);
                printf("Melhor %s geral plus:", class);
                for (k=0;k<RANK;k++)
                {
                        libplayer_getteamname(team, best_teambyclass[j][k], 20);
                        printf("\t%10s", team);
                }
                printf("\n");
        }
}

static int evolucao(int rodadas)
{
    int i, j, k, m, ret, jogou_emcasa;
    char team[20];
    char class[20];
    int best_class[CLASS_TOTAL][RANK] = {};
    int best_teambyclass[CLASS_TOTAL][RANK] = {};
    int best_class_home[CLASS_TOTAL] = {};
    int best_teambyclass_home[CLASS_TOTAL] = {};
    int best_class_out[CLASS_TOTAL] = {};
    int best_teambyclass_out[CLASS_TOTAL] = {};
    player **list;
    player *pl;

    for (i = 1; i < TOTALTEAMS; i++)
    {
    	libplayer_getteamname(team, i, 20);
    	printf("\n%s: ", team);
		for (j = 1; j < CLASS_TOTAL; j++)
		{
			list = get_list_byteamandclass(i, j);
			libplayer_getclassname(class, j, 20);
			printf("\n\t%s: ", class);
			if (list[0] == NULL)
			{
				printf("no player registered");
				continue;
			}
			for (k=7; k <= rodadas; k++)
			{
//				for (m = 0; list[m]; m++)
//				{
//					if (list[m]->statistics[rodadas-1])
//					{
//						jogou_emcasa = list[m]->statistics[rodadas-1]->JOGOU_EM_CASA;
//					}
//				}
				printf("\tR%d:", k);
				ret = pontuacao_media2(list, 1, k , k);
				printf("%4d...", ret);
				ret = pontuacao_media2(list, 0, k , k);
				printf("%d", ret);
			}
		}
		}
		printf("\n");
}

static int print_player(char *pteam, char *pclass, char *pname, int rodadas){
	char tmp[1024];
	char team[20];
	char class[20];
	player *pplayer = NULL;
	player **list, **classllist;
	int plposition, j, ret;

	strcpy(tmp, pclass);
	strcat(tmp, "\t");
	strcat(tmp, pname);
	strcat(tmp, "\t");
	strcat(tmp, pteam);

	pplayer = libplayer_parseplayer(tmp);

	if (pplayer == NULL)
	{
		printf("Error %s\n", __FUNCTION__);
		return 0;
	}

	classllist = get_list_byclass(pplayer->class);
	plposition = libplayer_findplayer(classllist, pplayer);
	list = libplayer_createlist();
	libplayer_appendtolist(&list, classllist[plposition]);

    for (j=2; j <= rodadas; j++)
    {
		if (list[0]->statistics[j-2])
		{
			ret = pontuacao_media2(list, list[0]->statistics[j-2]->JOGOU_EM_CASA, j, j);
			printf("\t\trodada: %2d, Media: %d", j, ret);
			if (list[0]->statistics[j-2]->JOGOU_EM_CASA)
			{
				libplayer_getteamname(team, list[0]->team, 20);
				printf("\t%s vs ", team);
				libplayer_getteamname(team, list[0]->statistics[j-2]->JOGOU_CONTRA, 20);
				printf("%s\n", team);
			} else {
				libplayer_getteamname(team, list[0]->statistics[j-2]->JOGOU_CONTRA, 20);
				printf("\t%s vs ", team);
				libplayer_getteamname(team, list[0]->team, 20);
				printf("%s\n", team);
			}
	}

    }

    ret = pontuacao_media_fora(list);
    printf("\tMedia de pontuacao jogando fora: %d\n", ret);
    ret = pontuacao_media_emcasa(list);
    printf("\tMedia de pontuacao jogando em casa: %d\n",  ret);
    ret = custo_medio(list);
    printf("\tMedia de custo: %d\n", ret);
}

/*
 *
 */
int main(int argc, char **args){
	player **list = NULL;
	int i, rodadas = 0;

	data_init();
//	load_file("rodada5", 5);rodadas=5;
//        load_file("rodada6", 6);rodadas=6;
	load_file("rodada7", 7);rodadas=7;
	load_file("rodada8", 8);rodadas=8;
	load_file("rodada9", 9);rodadas=9;
	load_file("rodada10", 10);rodadas=10;
        load_file("rodada11", 11);rodadas=11;
        load_file("rodada12", 12);rodadas=12;
        load_file("rodada13", 13);rodadas=13;

	list = get_list_byclass(CLASSLESS);
	for (i = 0; list[i]; i++)
		libplayer_cleanstatistics(list[i]);

        if (argc == 4){
        	print_player(args[1], args[2], args[3], rodadas);
        	exit(0);
        }

//	print_list(get_list_byclass(GOLEIRO));

//        print_general_statistics();
//        print_teams_statistics(rodadas);
	print_bestclassbet_by_team(rodadas);
	print_bestplayerbet_by_class(rodadas);
        print_bestclassbet_by_playedagainst(rodadas);
        print_bestclassbet_by_teamplus(rodadas);
//        evolucao(rodadas);
//	sleep(100);
	return 0;
}
